package com.ipharma.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import oracle.jdeveloper.layout.GridBagConstraints2;

import com.ipharma.McdError;
import com.ipharma.McdPrs;
import com.ipharma.dto.Product;

/**
 * A Swing-based dialog class.
 * <P>
 * @author JorgE.H
 */
public class GUIConfirmer extends GUIDialog
{
  /**
   * The layout used by the main panel.
   */
  GridBagLayout layoutBase = new GridBagLayout();

  /**
   * The main panel.
   */
  JPanel jPanelBase = new JPanel();
  GUIButtonImage jButtOk;
  GUIButtonImage jButtCancel;
  JLabel jLblQuest = new JLabel();

  /**
   * Attributes
   */
  Product product;
  boolean option = false;
  GUIOperation myContainer;

  /**
   * Constructs a new instance.
   * @param parent
   * @param title
   * @param modal
   */
  public GUIConfirmer( GUIOperation pContainer, String title, boolean modal )
  {
    super( pContainer.myContainer.mcdMain.getFrame(), title, modal );
    try
    {
      myContainer = pContainer;
      jbInit();
      pack();
    }
    catch ( Exception e )
    {
      McdError.add( e );
    }
  }

  /**
   * Initializes the state of this instance.
   */
  private void jbInit() throws Exception
  {
    jPanelBase.setLayout( layoutBase );
    jPanelBase.setBackground( Colors.CFR_BACK );
    jPanelBase.setPreferredSize( McdPrs.dialogSize );
    jLblQuest.setHorizontalAlignment( SwingConstants.CENTER );

    // option buttons
    jButtOk = new GUIButtonImage( Texts.BTN_SI, Images.buttYes, Images.buttYesDn );
    jButtOk.setFont( Texts.fntKey );
    jButtOk.setForeground( Colors.CFR_TEXT );
    jButtOk.addActionListener( new GUIConfirmer_jButtOk_actionAdapter( this ) );
    jButtCancel = new GUIButtonImage( Texts.BTN_NO, Images.buttNo, Images.buttNoDn );
    jButtCancel.setFont( Texts.fntKey );
    jButtCancel.setForeground( Colors.CFR_TEXT );
    jButtCancel.addActionListener( new GUIConfirmer_jButtCancel_actionAdapter( this ) );

    jPanelBase.add( jLblQuest, new GridBagConstraints2( 0, 0, 2, 1, 1.0, 1.0,
        GridBagConstraints.SOUTH, GridBagConstraints.NONE, Texts.dialogMargin, 0, 0 ) );
    jPanelBase.add( jButtOk, new GridBagConstraints2( 0, 1, 1, 1, 1.0, 1.0,
        GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, Texts.dialogMargin, 0, 0 ) );
    jPanelBase.add( jButtCancel, new GridBagConstraints2( 1, 1, 1, 1, 1.0, 1.0,
        GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, Texts.dialogMargin, 0, 0 ) );

    getContentPane().add( jPanelBase );
  }

  /**
   * Sets question for confirmation. Allows html code for text.
   * @param pQuest text for question
   */
  public void setQuestion( String pQuest )
  {
    jLblQuest.setText( "<html><font size='4' face='Verdana, Arial, Helvetica, sans-serif' color='#"
        + Integer.toHexString( Colors.CFR_HT_TEXT )
        + "'><b>" + pQuest + "</b></font></html>" );
  }

 /**
   * Returns selected value
   * @return boolean selected value
   */
  public boolean getSelection()
  {
    return option;
  }

  void jButtCancel_actionPerformed( ActionEvent e )
  {
    option = false;
    this.stop();
  }

  void jButtOk_actionPerformed(ActionEvent e)
  {
    option = true;
    this.stop();
  }

}

class GUIConfirmer_jButtCancel_actionAdapter implements java.awt.event.ActionListener
{
  GUIConfirmer adaptee;

  GUIConfirmer_jButtCancel_actionAdapter( GUIConfirmer adaptee )
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed( ActionEvent e )
  {
    adaptee.jButtCancel_actionPerformed( e );
  }
}

class GUIConfirmer_jButtOk_actionAdapter implements java.awt.event.ActionListener
{
  GUIConfirmer adaptee;

  GUIConfirmer_jButtOk_actionAdapter(GUIConfirmer adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.jButtOk_actionPerformed(e);
  }
}